# Makefile 文件，用于自动化管理 Go 项目的构建、测试、部署等任务

# 变量定义
# 项目名，一般和git仓库同名
PROJECT_NAME=fiberx
MAIN_FILE=main.go

PKG := "$(PROJECT_NAME)"
MOD_DIR := $(shell go env GOMODCACHE)
# go list gitee.com/whouys/restful-api-demo/...
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)


# 版本信息
BUILD_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
BUILD_COMMIT := ${shell git rev-parse HEAD}
BUILD_TIME := ${shell date '+%Y-%m-%d %H:%M:%S'}
BUILD_GO_VERSION := $(shell go version | grep -o 'go[0-9].[0-9].*')
VERSION_PATH := "${PKG}/version"
PLATFORM := ${shell uname -m}

# 使用git tag作为版本号
VERSION-TAG = ${shell git tag|tail -1}

# 伪目标声明
.PHONY: all dep lint vet test test-coverage build clean

# 规则定义
all: build

dep: ## Get the dependencies
	@go mod tidy

lint: ## Lint Golang files
	@golint -set_exit_status ${PKG_LIST}

vet: ## Run go vet
	@go vet ${PKG_LIST}

test: ## Run unittests
	@go test -short ${PKG_LIST} -v -count=1

test-coverage: ## Run tests with coverage
	@go test -short -coverprofile cover.out -covermode=atomic ${PKG_LIST} 
	@cat cover.out >> coverage.txt

build: dep ## Build the binary file
	@go build -ldflags "-s -w" -o dist/${PROJECT_NAME}_${PLATFORM}_$(VERSION-TAG) $(MAIN_FILE)

linux: dep ## Build the binary file
	@GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -ldflags "-X '${VERSION_PATH}.GIT_BRANCH=${BUILD_BRANCH}' -X '${VERSION_PATH}.GIT_COMMIT=${BUILD_COMMIT}' -X '${VERSION_PATH}.BUILD_TIME=${BUILD_TIME}' -X '${VERSION_PATH}.GO_VERSION=${BUILD_GO_VERSION}' -X '${VERSION_PATH}.GIT_TAG=${VERSION-TAG}'"  -o dist/${PROJECT_NAME}_linux_${PLATFORM}_$(VERSION-TAG) $(MAIN_FILE)

linux-static: dep ## Build the binary file
	@GOOS=linux GOARCH=amd64 go build -ldflags '-linkmode "external" -extldflags "-static"' -ldflags "-s -w" -ldflags "-X '${VERSION_PATH}.GIT_BRANCH=${BUILD_BRANCH}' -X '${VERSION_PATH}.GIT_COMMIT=${BUILD_COMMIT}' -X '${VERSION_PATH}.BUILD_TIME=${BUILD_TIME}' -X '${VERSION_PATH}.GO_VERSION=${BUILD_GO_VERSION}' -X '${VERSION_PATH}.GIT_TAG=${VERSION-TAG}'"  -o dist/${PROJECT_NAME}_linux_${PLATFORM}_$(VERSION-TAG) $(MAIN_FILE)

windows: dep ## Build the binary file
	@GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -ldflags "-X '${VERSION_PATH}.GIT_BRANCH=${BUILD_BRANCH}' -X '${VERSION_PATH}.GIT_COMMIT=${BUILD_COMMIT}' -X '${VERSION_PATH}.BUILD_TIME=${BUILD_TIME}' -X '${VERSION_PATH}.GO_VERSION=${BUILD_GO_VERSION}' -X '${VERSION_PATH}.GIT_TAG=${VERSION-TAG}'"  -o dist/${PROJECT_NAME}_windows_${PLATFORM}_$(VERSION-TAG).exe $(MAIN_FILE)

run: # Run Develop server
	@go run $(MAIN_FILE)

clean: ## Remove previous build
	@rm -rf dist/*
	@rm -rf tmp/*

push: # push git to multi repo
	@git push

help: ## Display this help screen
	@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

kill: # kill air run server
	@ps aux|grep air|grep -v "grep"|awk '{print $$2}'|xargs kill -9;echo killed



m: # migrate迁移表
	@go mod tidy
	@go run hack/dbtool/main.go -m

g: # gen生成dao和model
	@go mod tidy
	@go run hack/dbtool/main.go -g